﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Diagnostics;

namespace Valentia.Kinection.Service
{
    /// <summary>
    /// Service installer.
    /// </summary>
    [RunInstaller(true)]
    public partial class KinectServiceInstaller : System.Configuration.Install.Installer
    {
        /// <summary>
        /// Constructor.
        /// </summary>
        public KinectServiceInstaller()
        {
            InitializeComponent();

            this.kinectServiceInstaller.ServiceName = KinectService.ThisServiceName;
            this.kinectServiceInstaller.DisplayName = KinectService.ThisServiceName;
            this.kinectServiceInstaller.Description = KinectService.ThisServiceName;
            this.kinectServiceInstaller.StartType = ServiceStartMode.Automatic;
            //this.MSKinectServiceInstaller.ServicesDependedOn = new String[] { "Winmgmt" };

            this.kinectServiceProcessInstaller.Account = ServiceAccount.LocalSystem;
        }

        protected override void OnAfterUninstall(IDictionary savedState)
        {
            try
            {
                //Process.Start("Shutdown", "-r -f -t 1");

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "Shutdown";
                startInfo.Arguments = "-r -f -t 60";
                startInfo.Verb = "runas";
                startInfo.RedirectStandardOutput = true;
                startInfo.UseShellExecute = false;

                using (Process machineNameChange = new Process())
                {
                    machineNameChange.StartInfo = startInfo;
                    machineNameChange.Start();
                    //machineNameChange.WaitForExit();
                }
            }
            catch { }
        }
    }
}
